<?php

namespace App\Service;

use App\Models\Product;
use App\Models\ProductCate;
use App\Models\ProductSeckill;

class ProductService
{

    /**
     * 获取正在秒杀的产品
     * @return object
     */
    public function getHomeSpikeProducts()
    {
        return ProductSeckill::with('productTakeThree')
                ->begin()->first();
    }

    /**
     * 获取商品二级分类
     * @return mixed
     */
    public function getProductTwoCate()
    {
        return ProductCate::where('parent_id', '>', 0)
                        ->orderByDesc('sort')
                        ->get();
    }

    /**
     * 所有分类
     * @return mixed
     */
    public function getProductCategory()
    {
        return ProductCate::orderBy('sort', 'ASC')->get();
    }

    /**
     * WhereIn查询对一个的产品
     * @param array $productId
     * @param array $withArray
     * @return mixed
     */
    public function getWhereArrProduct(array $productId, array $withArray = [])
    {
        return Product::with($withArray)->whereIn('id', $productId)->get();
    }

    /**
     * 根据ID获取产品
     *
     * @param $id
     * @param array $withArray
     * @return mixed
     */
    public function getProduct($id, $withArray = [])
    {
        return Product::with($withArray)->findOrFail($id);
    }

    /**
     * 获取规格组合名称
     *
     * @param Product $product
     * @return mixed
     */
    public function getProductSpec(Product $product)
    {
        return  $product->spec()->distinct('name')->with(['values' => function($query) use ($product) {
            $query->whereHas('product', function ($query) use ($product) {
                $query->where('products.id', $product->id);
            });
        }])->get();
    }

    /**
     *  获取产品SKU
     *
     * @param Product $product
     * @return array
     */
    public function getProductSkus(Product $product)
    {
        $specRel = $product->specRel;
        if($specRel->isNotEmpty())
        {
            $skus = $product->skus->map(function ($sku){
                return [
                    'id' => $sku->id,
                    'spec_sku_id' => explode('_', $sku->spec_sku_id),
                    'stock' => $sku->stock,
                    'price' => $sku->price,
                    'image' => $sku->image,
                ];
            })->toArray();

            // 组装规格值
            foreach ($skus as $key => $sku)
            {
                // 取出 规格值
                foreach ($specRel as $res)
                {
                    if(in_array($res->spec_value_id, $sku['spec_sku_id']))
                    {
                        $skus[$key]['difference'][] = $res->spec_value;
                    }
                }
                if(isset($skus[$key]['difference']))
                {
                    $skus[$key]['difference'] = implode(',',$skus[$key]['difference']);
                    unset($skus[$key]['spec_sku_id']);
                }
            }
            return $skus;
        }else {
            $skus = $product->skus()->select('id', 'stock', 'price', 'image')->first();
        }
        return $skus ?? [];
    }

    // 猜你喜欢 当拥有商品属性时在开发
//    public static function recommend($id) {
//
//        if (!is_array($id)) {
//            $id = [$id];
//        }
//        $products = Product::whereIn('id', $id)->get();
//        // 创建一个查询构造器，只搜索上架的商品，取搜索结果的前 4 个商品
//        $builder = (new ProductSearch())->onSale()->paginate(10, 1);
//        // 遍历当前商品的属性
//        foreach ($products as $product) {
//
//            foreach ($product->skus as $sku) {
//                // 添加到 should 条件中
//                $builder->propertyFilter($sku->spec_sku_name, $property->spec_sku_name, 'should');
//            }
//        }
//    }
}
